package cn.zsc.netdisk.controller;

import cn.zsc.netdisk.pojo.User;
import cn.zsc.netdisk.response.Code;
import cn.zsc.netdisk.response.Result;
import cn.zsc.netdisk.service.MobileCodeService;
import cn.zsc.netdisk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ccying
 * @date 2021/2/9
 */
@RestController
@Slf4j
@RequestMapping("/")
public class UserController {
    private final UserService service;
    private final MobileCodeService mobileCodeService;

    public UserController(UserService service, MobileCodeService mobileCodeService) {
        this.service = service;
        this.mobileCodeService = mobileCodeService;
    }

    /**
     * 请求验证用户是否已登录验证
     * */
    @CrossOrigin
    @GetMapping("/verify")
    public Result verify() {
        return Result.success();
    }

    /**
     * 请求验证用户账号是否可以注册
     * @param uid 用户账号
     * */
    @CrossOrigin
    @GetMapping("/sign-up/verify/uid")
    public Result verifyUid(@RequestParam String uid) {
        System.out.println("*************************************************");
        log.info(String.format("请求验证用户账号[%s]是否可以注册", uid));
        if (this.service.findUserByUid(uid) == null) {
           log.info(String.format("用户账号【%s】未注册", uid));
           return Result.success();
        } else {
            log.info(String.format("用户账号【%s】已经注册", uid));
           return Result.fail(Code.USER_ACCOUNT_EARLY_EXIST);
        }
    }

    /**
     * 请求验证手机号码是否已被绑定用户账号
     * */
    @CrossOrigin
    @GetMapping("/sign-up/verify/mobileNumber")
    public Result verifyMobileNumber(@RequestParam String mobileNumber) {
        System.out.println("*************************************************");
        log.info(String.format("请求验证手机号码【%s】是否已被绑定用户账号", mobileNumber));

        if (this.service.findUserByMobile(mobileNumber) == null) {
            log.info(String.format("手机号码【%s】未绑定用户账号", mobileNumber));
            return Result.success();
        } else {
            log.info(String.format("手机号码【%s】已经绑定用户账号", mobileNumber));
            return Result.fail(Code.USER_MOBILE_NUMBER_ERROR);
        }
    }

    /**
     * 请求注册用户
     * @param uid 用户账号
     * @param password 登录密码
     * @param mobileNumber 手机号码
     * @param mobileCode 手机验证码
     * */
    @CrossOrigin
    @PostMapping("/sign-up")
    public Result registry(@RequestParam String uid, @RequestParam String password, @RequestParam String mobileNumber, @RequestParam int mobileCode) {
        System.out.println("*************************************************");
        log.info("请求注册用户");
        if (this.service.findUserByUid(uid) != null) {
            log.error("用户已注册");
            return Result.fail(Code.USER_ACCOUNT_EARLY_EXIST);
        }
        if (this.service.findUserByMobile(mobileNumber) != null) {
            log.error("手机号已被绑定");
            return Result.fail(Code.USER_MOBILE_NUMBER_ERROR);
        }
        int code = this.mobileCodeService.get(mobileNumber);
        if (!(code != -1 && code == mobileCode)) {
            log.error("手机验证码已过期");
            return Result.fail(Code.USER_MOBILE_CODE_ERROR);
        }
        try {
            this.service.addUser(uid, mobileNumber, password);
            return Result.success();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求修改用户头像
     * @param file 文件
     * */
    @CrossOrigin
    @PostMapping("/reset-avatar")
    public Result resetAvatar(MultipartFile file) {
        System.out.println("*************************************************");
        log.info("请求修改用户头像");
        try {
            this.service.updateAvatar(file);
            return Result.success();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求修改用户密码
     * @param mobileNumber 手机号码
     * @param mobileCode 手机验证码
     * @param password 密码
     * */
    @CrossOrigin
    @PostMapping("/reset-password")
    public Result resetPassword(@RequestParam String mobileNumber, @RequestParam int mobileCode, @RequestParam String password) {
        System.out.println("*************************************************");
        log.info("请求修改用户密码");
        int code = this.mobileCodeService.get(mobileNumber);
        if (!(code != -1 && code == mobileCode)) {
            log.error("手机验证码过期");
            return Result.fail(Code.USER_MOBILE_CODE_ERROR);
        }
        User user = this.service.findUserByMobile(mobileNumber);
        if (user == null) {
            log.error("用户账号不存在");
            return Result.fail(Code.USER_ACCOUNT_NOT_FOUND);
        }
        try {
            user = this.service.updatePasswordByMobile(mobileNumber, password);
            return Result.success(user);
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求修改用户手机号码
     * @param mobileNumber 新手机号码
     * @param mobileCode 验证码
     * */
    @CrossOrigin
    @PostMapping("/reset-mobile")
    public Result resetMobile(@RequestParam String mobileNumber, @RequestParam int mobileCode) {
        System.out.println("*************************************************");
        log.info("请求修改用户手机号码");
        if (this.service.findUserByMobile(mobileNumber) != null) {
            log.error("手机号已被绑定");
            return Result.fail(Code.USER_MOBILE_NUMBER_ERROR);
        }
        int code = this.mobileCodeService.get(mobileNumber);
        if (!(code != -1 && code == mobileCode)) {
            log.error("手机验证码过期");
            return Result.fail(Code.USER_MOBILE_CODE_ERROR);
        }
        try {
            User user = this.service.updateMobile(mobileNumber);
            return Result.success(user);
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求修改用户电子邮箱
     * */
    @CrossOrigin
    @PostMapping("/reset-email")
    public Result resetEmail(@RequestParam String email) {
        System.out.println("*************************************************");
        log.info("请求修改用户电子邮箱");
        try {
            User user = this.service.updateEmail(email);
            return Result.success(user);
        } catch (Exception e) {
            log.info(e.toString());
            return Result.fail(Code.ERROR);
        }
    }
}
